import http from "@/common/http.js"
import cache from "@/common/cache.js"
import tool from "@/common/tool.js"


function getUserId() {
	let userId = cache.get('user_id')
	if (userId) {
		return userId;
	}

	uni.reLaunch({
		url: '/pages/login/login'
	})
}


function getInfo(call) {
	let userId = getUserId();
	http.request({
		url: '/api/v1/getUserInfo',
		method: 'POST',
		data: {
			userId: userId
		},
		success: ret => {
			call(ret);
		},
		fail: ret => {
			uni.showToast({
				icon: 'none',
				title: '请登录'
			})
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}
	})
}

// POST /api/v1/getRealNameInfo 获取用户实名认证
function getRealNameInfo(call) {
	http.request({
		url: '/api/v1/getRealNameInfo',
		data: {
			userId: getUserId()
		},
		success: ret => {
			call(ret);
		},
		fail: ret => {
			uni.showToast({
				icon: 'none',
				title: '获取实名认证状态失败'
			})
		}

	})
}


// POST /api/v1/getAmountDetail 
function getAmountDetail(no,id,call) {
	http.request({
		url: '/api/v1/getAmountDetail',
		data: {
			id:id,
			no:no,
			userId: getUserId()
		},
		success: ret => {
			call(ret);
		},
		fail: ret => {
			uni.showToast({
				icon: 'none',
				title: '获取收益详情失败'
			})
		}

	})
}



function getVerifyCode(mobile,type, call) {
	http.request({
		url: '/api/v1/getVerifyCode',
		method: 'get',
		data: {
			mobile: mobile,
			type:type
		},
		success: ret => {
			console.log(ret);
			call(ret);
		},
		fail: ret => {
			console.log(ret);
			uni.showToast({
				icon: 'none',
				duration:5000,
				// title: '获取验证码失败'
				title: ret.errorMessage
			})
		}
	})
}



function verifyCode(mobile,code, call) {
	console.log(mobile,code)
	http.request({
		url: '/api/v1/verifyCode',
		method: 'POST',
		params: {
			mobile: mobile,
			code:code
		},
		success: ret => {
			console.log(ret);
			call(ret);
		},
		fail: ret => {
			console.log(ret);
			uni.showToast({
				icon: 'none',
				title: '验证码输入错误'
			})
		}
	})
}

function getSign(call) {

	let sign = cache.get('local_sign');
	if (sign) {
		call(sign);
		return;
	}

	http.request({
		url: '/api/v1/getSign',
		method: 'get',
		success: ret => {
			console.log(ret);
			cache.set('local_sign', ret, 86400);
			call(ret);
		},
		fail: ret => {
			console.log(ret);
			uni.showToast({
				icon: 'none',
				title: '获取签名失败'
			})
		}
	})
}

function resetPassword(data, call) {
	http.request({
		url: '/api/v1/resetPassword',
		method: 'POST',
		type: 'form-data',
		data: data,
		success: ret => {
			call(ret);
		},
		fail: ret => {
			uni.showToast({
				icon: 'none',
				title: '重置密码失败'
			})
		}
	})
}




/**
 * POST /api/v1/getAmountRecord 现金收益明细
 * @param {
	 startDate: 2022-08-15
	 endDate: 2022-08-15
	 page: 1
 } params 
 * @param {*} call 
 */
function getAmountRecord(params, call) {
	params['userId'] = getUserId();
	// params['boundStatus'] = '';
	params['current'] = params.page;
	params['size'] = 20;

	getSign(secret => {
		params['secret'] = secret;
		console.log(params)
		http.request({
			url: '/api/v1/getAmountRecord',
			data: params,
			success: ret => {
				call(ret);
			},
			fail: ret => {
				console.log(ret);
				uni.showToast({
					icon: 'none',
					title: '获取现金明细失败'
				})
			}
		})

	})
}

function getMemberAmount(call) {
	http.request({
		url: '/api/v1/getMemberAmount',
		data: {
			userId: getUserId()
		},
		success: ret => {
			call(ret);
		}
	})
}





/**
 * POST /api/v1/withdraw 用户提现接口
 * @param {
	 startDate: 2022-08-15
	 endDate: 2022-08-15
	 current: 1
 } params 
 * @param {*} call 
 */
function withdraw(params, call) {
	params['userId'] = getUserId();
	getSign(secret => {
		params['secret'] = secret;
		console.log(params)
		http.request({
			url: '/api/v1/withdraw',
			data: params,
			success: ret => {
				call();
			},
			fail: ret => {
				console.log(ret);
				uni.showModal({
					showCancel:false,
					title: '申请提现失败',
					content:ret.errorMessage
				})
			}
		})

	})
}


function getMemberInvite(call){
	http.request({
		url: '/api/v1/getMemberInvite',
		data: {
			userId: getUserId()
		},
		success: ret => {
			call(ret);
		},
		fail: ret => {
			console.log(ret);
			uni.showToast({
				icon: 'none',
				title: '获取用户邀请信息失败'
			})
		}
	})
}



// POST /api/v1/getWithdrawRecord
function getWithdrawRecord(page, startDate, endDate, call) {
	getSign(secret => {
		let date = new Date();
		let data = {
			"current": page,
			"size": 20,
			"secret": secret,
			"startDate": startDate, // 格式 2022-04-06
			"endDate": endDate, // 格式 2022-04-06
			"userId": getUserId(),
			"boundStatus": 1,
		}

		http.request({
			url: '/api/v1/getWithdrawRecord',
			data: data,
			success: ret => {
				call(ret);
			},
			fail: ret => {
				console.log(ret);
				uni.showToast({
					icon: 'none',
					title: '获取提现信息失败'
				})
			}
		})

	})


}

export default {
	getMemberInvite: getMemberInvite,
	verifyCode: verifyCode,
	getUserId: getUserId,
	getRealNameInfo: getRealNameInfo,
	getInfo: getInfo,
	getVerifyCode: getVerifyCode,
	getSign: getSign,
	resetPassword: resetPassword,
	getMemberAmount: getMemberAmount,
	getAmountRecord: getAmountRecord,
	withdraw: withdraw,
	getWithdrawRecord: getWithdrawRecord,
	getAmountDetail:getAmountDetail
}
